package cn.csust.net2.manager.client;


import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.shared.service.LoginService;
import cn.csust.net2.manager.shared.service.LoginServiceAsync;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.vo.LoginVo;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class Login extends Window {
	
	LoginServiceAsync loginService = GWT.create(LoginService.class);
	private TextField<String> userNumber;
	private TextField<String> password;
	private RadioGroup radioGroup;
	//事件的分发句柄
	public HandlerManager eventBus;

	public Login(HandlerManager eventBus) {
		this.eventBus = eventBus;
		initLoginPanel();
		show();
	}

	public void initLoginPanel() {

		this.setClosable(false);
		this.setHeading("登陆");
		this.setLayout(new FlowLayout());
		this.setResizable(false);

		FormData formData = new FormData("40%");

		FormPanel simple = new FormPanel();
		simple.setHeaderVisible(false);
		simple.setFrame(true);
		simple.setLabelWidth(50);
		//账户密码显示组件
		userNumber = new TextField<String>();
		userNumber.setFieldLabel("账号");
		userNumber.setValue("207");
		userNumber.setAllowBlank(false);
		userNumber.getFocusSupport().setPreviousId(simple.getButtonBar().getId());
		simple.add(userNumber, formData);

		password = new TextField<String>();
		password.setPassword(true);
		password.setValue("207");
		password.setFieldLabel("密码");
		password.setAllowBlank(false);
		simple.add(password, formData);
		Button submit = new Button("登陆");
		Button regist = new Button("重置");
		//类型选择显示组件
	    Radio radio = new Radio();  
		radio.setBoxLabel(Constant.LOGIN_TYPE_STUDENT);  
		radio.setValue(true);  
		Radio radio1 = new Radio();  
		radio1.setBoxLabel(Constant.LOGIN_TYPE_TEACHER);
		Radio radio2 = new Radio();  
		radio2.setBoxLabel(Constant.LOGIN_TYPE_PARENT);  
		radioGroup = new RadioGroup();  
		radioGroup.setFieldLabel("类型");  
		radioGroup.add(radio);  
		radioGroup.add(radio1);
		radioGroup.add(radio2);  
		simple.add(radioGroup, formData);
		submit.addSelectionListener(new LoginListener());
		regist.addSelectionListener(new RegistListener());
		simple.addButton(submit);
		simple.addButton(regist);

		simple.setButtonAlign(HorizontalAlignment.CENTER);

		 FormButtonBinding binding = new FormButtonBinding(simple);
		 binding.addButton(submit);

		this.add(simple);
	}
	
	/*
	 * 登陆监听
	 */
	class LoginListener extends SelectionListener<ButtonEvent> {

		@Override
		public void componentSelected(ButtonEvent ce) {
			hide();
			final MessageBox waitMessageBox = MessageBox.wait("加载进度","正在加载系统，请稍后", "加载中...");      
			String username = userNumber.getValue();
			String pass = password.getValue();
			String type = radioGroup.getValue().getBoxLabel();
		//	MessageBox.alert("警告",  type, null); 
			loginService.login(username, pass,type, new AsyncCallback<LoginVo>(){
				
				@Override
				public void onFailure(Throwable caught) {
					
					MessageBox.alert("系统信息", "连接过程发现错误", new Listener<MessageBoxEvent>(){
						@Override
						public void handleEvent(MessageBoxEvent be) {
								show();
						}
						
					});
				}

				@Override
				public void onSuccess(LoginVo result) {
					
					if(result.getUser()!=null){
						//登陆成功、
						App.user = result.getUser();
						if(result.getNowTerm()!=null&&!result.getNowTerm().equals(""))
							App.nowTerm = result.getNowTerm();
//						Login.this.hide();
						App app = new App(eventBus,Utils.changeToMap(result.getAuthorities()));
						waitMessageBox.close();
						RootPanel.get().add(app);
						
					}else{
						MessageBox.alert("系统信息", "账号或密码错误", new Listener<MessageBoxEvent>(){
							@Override
							public void handleEvent(MessageBoxEvent be) {
									show();
							}
							
						});
					}
				}
				
			});
		}
	}

	/*
	 * 注册监听
	 */
	class RegistListener extends SelectionListener<ButtonEvent> {

		@Override
		public void componentSelected(ButtonEvent ce) {	
			userNumber.clear();
			password.clear();
		}
	}




}
